<template>
  <base-breadcrumb>
    <searchData :searchData="searchArr" @search="search"></searchData>

    <a-card :bordered="false" style="over-flow:scroll">
      <!-- <title-name title="设计策划书列表"></title-name> -->
      <div>
        <a-row>
          <a-col :md="24" :sm="19">
            <list-table
                ref="table"
                class="export-table"
                :columns="columns"
                :start-index="0"
                :request="loadData"
                :statistics-config="{ use: true, total, temp }"
                :export-config="{
                    use: true,
                    select: $route.meta.title,
                    all: $route.meta.title
                }"
            >
              <a-button slot="prefix" type="primary"  @click="goAdd('add')" v-btnPermission="'ds_002_add_01'">新增设计策划书</a-button>
              <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
              <span slot="score" slot-scope="text, record" v-if="record.status == '审核通过'">
                  {{getScore(record)}}
              </span>
              <span slot="actions" slot-scope="text, record">
                <MoreButton>
                <a @click="handleSub(record,'view')" v-btnPermission="'ds_002_loo_02'">查看</a>
                <a v-if="record.statusCode=='0' || record.statusCode=='3'" @click="handleEdit(record,'edit')" v-btnPermission="'ds_002_edi_03'">编辑</a>
                <a v-btn-permission="'ds_002_alk_07'" @click="inStore(record)"
                v-if="record.statusCode=='2'&&(!record.knowledgeStatus || record.knowledgeStatus=='DRAFT' || record.knowledgeStatus=='REJECTED')"
                >纳入案例库</a>
                <a v-if="record.statusCode !='0'"  @click="handleApproveDetail(record)" v-btnPermission="'ds_002_moi_05'">审批信息</a>
                </MoreButton>
              </span>
            </list-table>
          </a-col>
        </a-row>
      </div>
          
        <!-- 审批详情 -->
        <ApprovalDetails
            :title="'审批详情'"
            :businessId="businessId"
            :tab-list="tabList"
            multiple
            :visible="visibleApprove"
            @cancel="visibleApprove = false"
            @confirm="visibleApprove = false"
        />
    </a-card>
  
  </base-breadcrumb>
</template>

<script>
  import Vue from 'vue'
  import { addEvaluateList } from '@/api/evaluate'
  import { getplotsManage , getplotsManageTotal   ,knowledgeStatus} from '@/api/design/designManage'
  import { getCompany } from '@/api/themeChoose/themeChoose'
  import  { getTypes} from '@/api/commons'
  import ApprovalDetails from '@/components/approvalDetails'
  import OrgTreeSelect from '@/components/OrgTreeSelect'
  const columns = [
    {
      title: '序号',
      width: '65px',
      scopedSlots: { customRender: 'serial' }
    },
    // {
    //   title: '设计策划书',
    //   width: '200px',
    //   dataIndex: 'name'
    // },
    {
      title: '分公司',
      width: '120px',
      dataIndex: 'branchCompany', 
    },
    {
      title: '项目',
      width: '200px',
      dataIndex: 'project', 
    },
    {
      title: '项目设计管理类别',
      width: '150px',
      dataIndex: 'designType',
    },
    // {
    //   title: '双优化立项计划表',
    //   width: '200px',
    //   dataIndex: 'optimization',
    // },
    {
      title: '创建人',
      width: '100px',
      dataIndex: 'person',
    },
    {
      title: '创建时间',
      width: '200px',
      dataIndex: 'time',
    },
    {
      title: '是否一次性审批通过',
      width: '100px',
      dataIndex: 'isAdopt',
    },
    {
      title: '评审分数',
      width: '100px',
      dataIndex: 'score',
      scopedSlots: { customRender: 'score' }
    },
    {
      title: '状态 ',
      width: '110px',
      dataIndex: 'status',
    },
    {
      title: '纳入案例库状态',
      dataIndex: 'knowledgeStatusName'
    },
    {
      title: '操作',
      dataIndex: 'actions',
      width: '150px',
      fixed:'right',
      scopedSlots: { customRender: 'actions' }
    }
  ]
  const auditStatusList = [
    { value: 'SUBMITTED', label: '纳入审核中' },
    { value: 'PAST', label: '纳入审核通过' },
    { value: 'REJECTED', label: '纳入审核不通过' }
  ]
  export default {
    name:'a'+ Date.now(),
    components:{
      ApprovalDetails
    },
    data(){
      this.columns = columns
      return{
        tabList: [],
        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
        // 格式化参数
        if (this.queryParam.time && this.queryParam.time.length > 0) {
          this.queryParam.startDate = this.queryParam.time[0].format('YYYY-MM-DD')
          this.queryParam.endDate = this.queryParam.time[1].format('YYYY-MM-DD')
          delete this.queryParam.time
        }

          if(this.queryParam.startDate) {
            this.queryParam.startDate = this.queryParam.startDate + ' 00:00:00'
          }
          if(this.queryParam.endDate) {
            this.queryParam.endDate = this.queryParam.endDate + ' 23:59:59'
          }
          const requestParameters = Object.assign({}, parameter, this.queryParam)
          this.getplotsManageTotal()
          return getplotsManage(requestParameters)
            .then(res => {
              res.data.forEach(item=>{
                let a = auditStatusList.find(ele => ele.value ==  item.knowledgeStatus)
                item.knowledgeStatusName = a ? a.label : '-'
              })
              this.totalSize = res.totalSize
              return res
            })
        },
        selectedRowKeys:'',
        selectedRows:'',
        confirmLoading:false,
        total:{},
        form:{
          name:'',
          status:''
        },
        businessId:'',
        visibleApprove:false,
        visible:false,
        advanced:false,
        totalSize:'',
        searchArr: [
          // 搜索框
          // {
          //   name: '设计策划书名称',
          //   value: 'name',
          //   type: 'input'
          // },
          {
            name: '状态',
            type: 'select',
            value: 'status',
            data: [
              {
                value: '0',
                label: '待提交审核'
              },
              {
                value: '1',
                label: '审核中'
              },
              {
                value: '2',
                label: '审核通过'
              },
              {
                value: '3',
                label: '审核不通过'
              }
            ]
          },
          {
            name: '纳入案例库状态',
            value: 'knowledgeStatus',
            type: 'select',
            data: [
              { value: '1', label: '纳入审核中' },
              { value: '2', label: '纳入审核通过' },
              { value: '3', label: '纳入审核不通过' }
            ]
          },
          {
            name: '时间阶段',
            value: 'time',
            type: 'dateRange'
          },
          {
            name: '所属单位',
            valueKey: 'searchQuery',
            type: OrgTreeSelect,
          },
          //                     {
          //   name: '分公司',
          //   valueKey: 'branchCompany',
          //   type: SelectSubCompany ,

          // },
          // {
          //   name: '分公司',
          //   value: 'branchCompany',
          //   type: 'select',
          //   data:[]
          // },
          {
            name: '是否一次性审批通过',
            type: 'select',
            value: 'isAdopt',
            data: [
              {
                value: '1',
                label: '是'
              },
              {
                value: '0',
                label: '否'
              }
            ],
          },
          {
            name: '项目设计管理类别',
            value: 'designType',
            type: 'select',
            data:[]
          },
        ],
      
        temp: () => {
            return (
                <span style="margin-right: 12px">
                  设计策划书总数：<span class="totalNum">{this.totalSize } 个 </span> |
                  待提交审核：<span class="totalNum">{this.total.draftNum } 个 </span> |
                  审核中：<span class="totalNum">{ this.total.submittedNum } 个 </span> |
                  审核不通过：<span class="totalNum">{ this.total.rejectedNum } 个 </span> |
                  审核通过：<span class="totalNum">{this.total.passNum } 个 </span> |
                  一次性审核通过：<span class="totalNum">{ this.total.firstPassNum } 个 </span> |
                  一次性审核通过率：<span class="totalNum">{ this.total.firstPassRate } </span>
                </span>
            )
        },
      }
    },
    computed: {
      // rowSelection() {
      //   return {
      //     selectedRowKeys: this.selectedRowKeys,
      //     selectedRows: this.selectedRows,   // 当前行所有数据
      //     onChange: this.onSelectChange
      //   }
      // }
    },
    created() {
      const userInfo = Vue.getAcloudProvider().getUserInfo()
      // this.getCompanyList({orgIds:[userInfo.orgId], tagCodes:['BC']})
      this.getTypes()
    },
    methods:{
      inStore(record) {
        let that = this
        this.$confirm({
          title: '提示',
          content: '请确认是否要将该设计策划书纳入案例库？',
          onOk() {
             return knowledgeStatus({id: record.id}).then(res=>{
               that.$message.success('提交成功')
               that.$refs.table.refresh(true)
             })
          },
          onCancel() {

          }
        })
      },
      //前端计划分数
      getScore(str){
        if(str.score){
          return str.score
        }else{
          if(str.auditScore && str.scoreNum){
             return (str.auditScore/str.scoreNum).toFixed(2);
          }
        }
      },
      // 分公司
      getCompanyList(params){
        getCompany(params).then(res=>{
          let companyList = []
          res.data.forEach(item=>{
            companyList.push({
              label:item.orgName,
              value:item.orgId
            })
          })
          this.searchArr[3].data = companyList
        })
      },
      handleApproveDetail(record){
        if(record.statusCode == 2 && record.knowledgeStatus) {
          this.tabList = [
            {
              name: '策划书管理流程',
              value: record.id
            },
            {
              name: '纳入案例库流程',
              value: record.id+'_knowledge'
            },
          ]
        } else {
          this.tabList = [
            {
              name: '策划书管理流程',
              value: record.id
            }
          ]
        }
        this.visibleApprove = true
        this.businessId = record.id
      },
      getTypes(){
        getTypes({type:'9'}).then(res=>{
          let list = []
          res.data.forEach(item=>{
            list.push({
              label:item.name,
              value:item.id
            })
          })
          this.searchArr[5].data = list
        })
      }, 
      toggleAdvanced(){
        this.advanced = !this.advanced
      },
      searchData(){

      },
      getplotsManageTotal(){
        getplotsManageTotal(this.queryParam).then((res)=>{
          this.total = res.data
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        console.log(selectedRowKeys)
        console.log(selectedRows)
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      search(values){
        this.queryParam = values
        this.queryParam.isAdopt=this.queryParam.isAdopt*1;
        this.$refs.table.refresh(true)
      },
      goAdd(type){
        this.$router.push({
          path:'/project/design/addDesignManage',
          query:{
            type:type
          }
        })
      },
      handleSub(record,type){
        this.$router.push({
          path:"/project/design/viewDesignManage",
          query:{
            id:record.id,
            type:type,
            status: record.statusCode
          }
        })
      },
      handleEdit(record,type){
        this.$router.push({
          path:"/project/design/editDesignManage",
          query:{
            id:record.id,
            type:type
          }
        })
      },
      handleAudit(record){
        this.$router.push({
          path:"/project/design/auditdesignManage",
          query:{
            id:record.id,
          }
        })
      },
      handleOk(){
        this.visible = false
        addEvaluateList(this.form).then((res)=>{
          if(res.code=='200'){
            this.$message.success("新增成功")
          }
          this.$refs.table.refresh(true)
        })
      },
      handleCancel(){
        this.visible = false
      }
    }
  }
</script>

<style scoped>
  .input{
    width:100%
  }
  .notShow{
    display:none
  }
</style>
